package com.atguigu.gmall.web.controller;

import com.atguigu.gmall.model.product.SkuInfo;
import com.atguigu.gmall.product.client.ProductFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author atguigu-mqx
 * @Description TODO
 * @Date 2023/5/23 14:10
 * @Version 1.0
 */
@Controller
public class CartController {

    @Autowired
    private ProductFeignClient productFeignClient;

    //  'http://cart.gmall.com/addCart.html?skuId=' + this.skuId + '&skuNum=' + this.skuNum

    /**
     * 添加购物车控制器
     * @param request
     * @return
     */
    @GetMapping("addCart.html")
    public String addCart(HttpServletRequest request){
        //  获取skuId
        String skuId = request.getParameter("skuId");
        String skuNum = request.getParameter("skuNum");
        //  调用远程方法
        SkuInfo skuInfo = this.productFeignClient.getSkuInfo(Long.parseLong(skuId));
        //  需要存储 skuInfo,skuNum
        request.setAttribute("skuInfo",skuInfo);
        request.setAttribute("skuNum",skuNum);

        //  返回添加成功页面。
        return "cart/addCart";
    }

    //  查看购物车列表 异步请求控制器获取数据
    @GetMapping("/cart.html")
    public String cartList(){
        //  返回购物车列表页面
        return "cart/index";
    }
}
